package com.mf.morefun.widget;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mf.morefun.MoreFunApplication;
import com.mf.morefun.R;
import com.zhcx.zhcxlibrary.utils.StringUtils;

public class NetErrorPop {
    private Context mContext;
    public NetErrorPop(Context mContext) {
        this.mContext = mContext;
    }
    public void showPop(String text){
        try {
            if (null != MoreFunApplication.mNetPop && MoreFunApplication.mNetPop.isShowing()){
                MoreFunApplication.mNetPop.dismiss();
                MoreFunApplication.mNetPop = null;
                showPop(text);
            }else {
                View view = LayoutInflater.from(mContext).inflate(R.layout.layout_neterror, null);
                RelativeLayout rl_bg = view.findViewById(R.id.rl_bg);
                rl_bg.getBackground().setAlpha(100);
                TextView tv_info = view.findViewById(R.id.tv_info);
                if (!StringUtils.isEmpty(text)){
                    tv_info.setText(text);
                }
                TextView tv_close = view.findViewById(R.id.tv_close);
                tv_close.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MoreFunApplication.mNetPop.dismiss();
                        MoreFunApplication.mNetPop = null;
                    }
                });
                MoreFunApplication.mNetPop = new PopupWindow(view, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
                MoreFunApplication.mNetPop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
                MoreFunApplication.mNetPop.setBackgroundDrawable(new BitmapDrawable());
                MoreFunApplication.mNetPop.setFocusable(true);
                MoreFunApplication.mNetPop.update();
                MoreFunApplication.mNetPop.showAtLocation(view, Gravity.CENTER, 0, 0);
                MoreFunApplication.mNetPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        MoreFunApplication.mNetPop.dismiss();
                        MoreFunApplication.mNetPop = null;
                    }
                });
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
